package org.jeecg.modules.monitoring.j_monitoring.controller;

import java.util.Arrays;
import java.util.Map;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.monitoring.j_monitoring.entity.GatewayWarningCount;
import org.jeecg.modules.monitoring.j_monitoring.entity.RingMonitor;
import org.jeecg.modules.monitoring.j_monitoring.entity.donghuan.ConditioningData;
import org.jeecg.modules.monitoring.j_monitoring.entity.donghuan.UpsData;
import org.jeecg.modules.monitoring.j_monitoring.service.IRingMonitorService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.monitoring.j_monitoring.service.impl.ItScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 动环监控
 * @Author: xiaoyq
 * @Date:   2023-07-12
 * @Version: V1.0
 */
@Api(tags="动环监控")
@RestController
@RequestMapping("/ringMonitor/ringMonitor")
@Slf4j
public class RingMonitorController extends JeecgController<RingMonitor, IRingMonitorService> {
	@Autowired
	private IRingMonitorService ringMonitorService;
	@Resource
	private ItScreenService screenService;
	/**
	 * 分页列表查询
	 *
	 * @param ringMonitor
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "动环监控-分页列表查询")
	//@RequiresPermissions("网络资源:查询")
	@ApiOperation(value="动环监控-分页列表查询", notes="动环监控-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RingMonitor>> queryPageList(RingMonitor ringMonitor,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RingMonitor> queryWrapper = QueryGenerator.initQueryWrapper(ringMonitor, req.getParameterMap());
		Page<RingMonitor> page = new Page<RingMonitor>(pageNo, pageSize);
		IPage<RingMonitor> pageList = ringMonitorService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param ringMonitor
	 * @return
	 */
	@AutoLog(value = "动环监控-添加")
	@ApiOperation(value="动环监控-添加", notes="动环监控-添加")
	//@RequiresPermissions("org.jeecg.modules.monitoring:动环监控:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RingMonitor ringMonitor) {
		ringMonitorService.save(ringMonitor);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param ringMonitor
	 * @return
	 */
	@AutoLog(value = "动环监控-编辑")
	@ApiOperation(value="动环监控-编辑", notes="动环监控-编辑")
	//@RequiresPermissions("org.jeecg.modules.monitoring:动环监控:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RingMonitor ringMonitor) {
		ringMonitorService.updateById(ringMonitor);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "动环监控-通过id删除")
	@ApiOperation(value="动环监控-通过id删除", notes="动环监控-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.monitoring:动环监控:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		ringMonitorService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "动环监控-批量删除")
	@ApiOperation(value="动环监控-批量删除", notes="动环监控-批量删除")
	//@RequiresPermissions("org.jeecg.modules.monitoring:动环监控:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.ringMonitorService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "动环监控-通过id查询")
	@ApiOperation(value="动环监控-通过id查询", notes="动环监控-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RingMonitor> queryById(@RequestParam(name="id",required=true) String id) {
		RingMonitor ringMonitor = ringMonitorService.getById(id);
		if(ringMonitor==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ringMonitor);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ringMonitor
    */
    //@RequiresPermissions("org.jeecg.modules.monitoring:动环监控:exportXls")
    @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
	@ApiOperation(value="动环监控-导出", notes="动环监控-导出")
    public ModelAndView exportXls(HttpServletRequest request, RingMonitor ringMonitor) {
        return super.exportXls(request, ringMonitor, RingMonitor.class, "动环监控");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("动环监控:importExcel")
	@ApiOperation(value="动环监控-导入", notes="动环监控-导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RingMonitor.class);
    }

	 @GetMapping("/test")
	 public Result<UpsData> test() {
//		 ringMonitorService.test();
		 String name = "艾默生UPS1";
		 UpsData data = ringMonitorService.getUpsData(name);
		 return Result.OK(data);
	 }


	 @ApiOperation(value="空调监控", notes="空调监控数据")
	 @GetMapping(value = "/conditioningData")
	 public Result<ConditioningData> conditioningData(@RequestParam(name="name",required=true) String name) {
		ConditioningData data = ringMonitorService.getConditioningData(name);
		 return Result.OK(data);
	 }


	 @ApiOperation(value="ups监控", notes="ups监控")
	 @GetMapping(value = "/upsData")
	 public Result<UpsData> upsData(@RequestParam(name="name",required=true) String name) {
		 UpsData data = ringMonitorService.getUpsData(name);
		 return Result.OK(data);
	 }

	 @ApiOperation(value = "gateway_warning-分页列表查询", notes = "gateway_warning-分页列表查询")
	 @GetMapping(value = "/getWarningCount")
	 public Result<GatewayWarningCount> getWarningCount(
			 @RequestParam(name = "name") String name) {

		 return Result.OK(ringMonitorService.getWarningCount(name));
	 }


	 @ApiOperation(value = "获取某个机房的温湿度", notes = "获取某个机房的温湿度")
	 @GetMapping(value = "/getTemHum")
	 public Result<Map<String,Object>> getTemHum(
			 @RequestParam(name = "name") String name) {

		 return Result.OK(screenService.getRoomTemHum(name));
	 }


}
